/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Client;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * This class should located in clients' part. so that clients can easily use hash method on it. 
 * Server only store the hashed string. (hashed from password)
 * As a result clients' password will never exposed by packets or somethings like that.
 * 
 * @author wyjo
 * This MessageDigest class provides applications the functionality of a message digest algorithm, such as MD5 or SHA. 
 * Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value.
 */
public class SecureHash {
    /**
     * wyjo
     * For the validation
     * 1-1. MD5 Encrpyt site
     * http://7thspace.com/webmaster_tools/online_md5_encoder.html
     * 1-2. MD5 Decrypt site
     * http://www.md5decrypt.org/
     * 
     * 2-1. SHA256 Encrpyt site
     * http://www.xorbin.com/tools/sha256-hash-calculator
     * 2-2. SHA256 Decrpyt site
     * SHA-256 is a cryptographic (one-way) hash function, so there is no direct way to decode it. 
     * The entire purpose of a cryptographic hash function is that you can't undo it.
     */
    
    public static void main(String args[]) {
        //Unit test for SecureHash class
        //Use Ctrl+F at console to match the hash code.
        
        //1. If passwords are different then Hashed code will be different
        //2. If passwords are same then Hashed code shell be same
        for (int i = 0; i < 2; i++) {
            String testPassword1 = "123456";
            System.out.println(testPassword1);

            System.out.println("MD5> " + SecureHash.hashMD5(testPassword1));//128 bit
            System.out.println("SHA256> " + SecureHash.hashSHA256(testPassword1));//256bit

            String testPassword2 = "hellowSecureHash123";
            System.out.println(testPassword2);

            System.out.println("MD5> " + SecureHash.hashMD5(testPassword2));
            System.out.println("SHA256> " + SecureHash.hashSHA256(testPassword2));
        }
    }
    // MD5
    public static String hashMD5(String str) {
        try {
            String MD5 = "";
            //Returns a MessageDigest object that implements the specified digest algorithm.
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte byteData[] = md.digest();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < byteData.length; i++) {
                sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }
            MD5 = sb.toString();
            return MD5;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        System.err.println("hashMD5("+str+") return null error");
        return null;
    }
    
    
// SHA-256
    public static String hashSHA256(String str) {
        try {
            String SHA = "";
            MessageDigest sh = MessageDigest.getInstance("SHA-256");
            sh.update(str.getBytes());
            byte byteData[] = sh.digest();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < byteData.length; i++) {
                sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }
            SHA = sb.toString();    
            return SHA;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        System.err.println("hashSHA256("+str+") return null error");
        return null;
    }
}
